//
//  TiCashViewController.m
//  1243
//
//  Created by 杨森 on 16/5/9.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "TiCashViewController.h"
#import "NetWorkTools.h"

#import "cashFirstTableViewCell.h"
#import "cashSecondTableViewCell.h"
#import "AddBankNumViewController.h"
#import "CardTypeTableViewController.h"
#import "ReadLoginData.h"
@interface TiCashViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,UIAlertViewDelegate,UIScrollViewDelegate>
{
    NSString *buyPassWord;
}
@property(nonatomic,strong)NSArray *cashArray;
@property (nonatomic,strong)NSDictionary *loadUserData;
@property(nonatomic,strong)UITableView *cashTableView;
@property(nonatomic,copy)NSString *strName;
@property (nonatomic, strong) NSString *cardInfo;
@property (nonatomic, strong) NSString *cardId;
@end

@implementation TiCashViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addSubViews];
    
    [self netWorkTools];
    [self data];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeCard:) name:@"changeCard" object:nil];
    //监听键盘的弹出
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChangeA:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)changeCard:(NSNotification *)noti {
    
    self.cardInfo = noti.userInfo[@"CardInfo"];
    self.cardId = noti.userInfo[@"CardId"];
    [self.cashTableView reloadData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
}

- (void)addSubViews
{
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    
    UITableView *cashTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WEITH, HEIGHT) style:UITableViewStyleGrouped];
    self.cashTableView = cashTableView;
    
    [self.view addSubview:cashTableView];
    self.view.backgroundColor = BACKGROUND_COLOR;
    
    cashTableView.delegate = self;
    cashTableView.dataSource = self;
    
    cashTableView.rowHeight = 60;
    self.title = @"提现";
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 2;
    }
    return 2;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            
            static NSString *identifier= @"cashFirstTableViewCell";
            
            cashFirstTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
            
            if (cell==nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"cashFirstTableViewCell" owner:nil options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            
           
            if (self.cashArray.count) {
                cell.bankNameNum.text = [NSString stringWithFormat:@"%@(%@)",self.cardInfo, self.cardId];
            }else{
                cell.bankNameNum.text = @"";
            }
            
            
            return cell;
            
        }else {
            
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"bankcell"];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"bankcell"] ;
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
//            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.text= @"添加银行卡";
            cell.imageView.image = [UIImage imageNamed:@"jia_03"];
            return cell;
        }
        
       
    }else{
        
        if (indexPath.row == 0) {
            
            static NSString *identifier= @"cashSecondTableViewCell";
            
            cashSecondTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
            
            if (cell==nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"cashSecondTableViewCell" owner:nil options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            cell.textField.delegate = self;
            self.strName  =  cell.textField.text;
            cell.labelTT.text = @"提现金额";
            cell.textField.keyboardType = UIKeyboardTypeNumberPad;
            cell.textField.tag = 1234321;
            return cell;
            
        }else{
            
            static NSString *identifier= @"cashSecondTableViewCell";
            
            cashSecondTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
            
            if (cell==nil) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"cashSecondTableViewCell" owner:nil options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            cell.textField.delegate = self;
            cell.labelTT.text = @"交易密码";
            buyPassWord  =  cell.textField.text;
            cell.textField.keyboardType = UIKeyboardTypeNumberPad;
            cell.textField.tag = 1234322;
            return cell;
            
            
        }
      
        
      
        
    }
    
    
}
- (void)data{
    NSDictionary *dict = @{
                           @"PhoneNum":[ReadLoginData readLoginData][@"UserName"],
                           @"Password":[ReadLoginData readLoginData][@"Password"]
                           };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"USERLOGIN" completeBlock:^(id object) {
        
        NSDictionary *loginDict = [NetWorkTools dictionaryWithJsonString:[object objectForKey:@"BackData"]];

        [ReadLoginData savelogindataToLocal:loginDict];
        
        NSLog(@"%@",loginDict);
        
        self.loadUserData = [ReadLoginData readLoginData];
        
        [self.cashTableView reloadData];
        

    }];
    
    
    
}
- (NSDictionary *)loadUserData {
    if (!_loadUserData) {
        _loadUserData = [NSDictionary dictionary];
    }
    return _loadUserData;
    
}


#pragma mark >>>>>>>>>>>delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row ==1) {
        
        AddBankNumViewController *addBank = [[AddBankNumViewController alloc] init];
        [self pushForm:self.navigationController pushToAndHidden:addBank];
    }else if (indexPath.row == 0){
        CardTypeTableViewController *a = [[CardTypeTableViewController alloc] init];
        [self pushForm:self.navigationController pushToAndHidden:a];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0.1;
    }
    return 90;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    }else{
        
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WEITH, 70)];
        bgView.backgroundColor = [UIColor whiteColor];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10, 140, 10)];
        label.text = [NSString stringWithFormat:@"可提现余额%.2f",[[self.loadUserData objectForKey:@"Money"] floatValue]];;
        
        label.textColor = [UIColor lightGrayColor];
        label.font = [UIFont systemFontOfSize:12];
        [bgView addSubview:label];
        
        
//        UILabel *number = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH - 100,10, 100, 10)];
////        number.text = @"明天24点前到账";
//        number.textColor = [UIColor lightGrayColor];
//        number.font = [UIFont systemFontOfSize:10];
//        [bgView addSubview:number];
//        number.text = [NSString stringWithFormat:@"余额%.2f",[[self.loadUserData objectForKey:@"Money"] floatValue]];
        
        
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [bgView addSubview:button];
        [button setBackgroundColor:[UIColor orangeColor]];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitle:@"提现" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(tiCashMoneyClick) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(10,CGRectGetMaxY(label.frame) + 10, WEITH-20, 40);
        
        
        return bgView;
    }
    
}

- (void)tiCashMoneyClick{
    
    [self netWorkToolsA];
    
    
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField.tag == 1234321) {
        NSString *newString = nil;
        newString = [textField.text stringByAppendingString:string];
        self.strName = newString;
        return YES;
    }else{
        NSString *newString = nil;
        newString = [textField.text stringByAppendingString:string];
        buyPassWord = newString;
        return YES;
    }
    
  
}

#pragma mark >>>>>>>获取银行卡号请求
- (void)netWorkTools {
    
    NSDictionary *dict = [ReadLoginData readLoginData];
    
    //    NSLog(@"%@",dict);
    
    NSString *userNumber = [dict objectForKey:@"Number"];
    
    //    NSString *buyPassWord = [dict objectForKey:@"Buy_Password"];
    
    NSDictionary *params = @{
                             @"UserNumber":userNumber,
                             };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"BANKINFOQUERY" completeBlock:^(id object) {
        NSLog(@"%@",object);
        NSString *string = [object objectForKey:@"BackData"];
        self.cashArray = [NetWorkTools arrayWithJsonString:string];
        
        NSString*string1 = self.cashArray[0][@"Bank_Code"];
        if (string1.length > 15) {
              string1 =  [string1 substringFromIndex:15];
        }
      
        NSLog(@"截取的值为：%@",string1);
        self.cardInfo = self.cashArray[0][@"Bank_Info"];
        self.cardId = string1;
        NSLog(@"%@",self.cashArray);
        [self.cashTableView reloadData];
        
    }];
}


#pragma mark >>>>>>>获取提现接口
- (void)netWorkToolsA {
    
    NSDictionary *dict = [ReadLoginData readLoginData];
    
    //    NSLog(@"%@",dict);
    
    NSString *userNumber = [dict objectForKey:@"Number"];
    
    NSString *buyPassWordA = [dict objectForKey:@"Buy_Password"];
    
    NSDictionary *params = @{
                             @"UserNumber":userNumber,
                             @"TiXianMoney":self.strName,
                             @"BusiPassword":buyPassWord,
                             @"Type":@"2"
                             };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"GUANGGAOFENHONGHUOXIANJINTIXIAN" completeBlock:^(id object) {
        NSLog(@"%@",object[@"Message"]);
      
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:object[@"Message"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        alert.tag = 10086;
        
        [alert show];
        
        
    }];
}
- (void)loadUserDataA {
    
    
    NSDictionary *dict = @{
                           @"PhoneNum":[ReadLoginData readLoginData][@"UserName"],
                           @"Password":[ReadLoginData readLoginData][@"Password"]
                           };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"USERLOGIN" completeBlock:^(id object) {
        
        NSDictionary *loginDict = [NetWorkTools dictionaryWithJsonString:[object objectForKey:@"BackData"]];
        
        [ReadLoginData savelogindataToLocal:loginDict];
        
        NSLog(@"%@",loginDict);
        
        self.loadUserData = [ReadLoginData readLoginData];
        
        [self.cashTableView reloadData];
        
        
    }];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 10086) {
        [self.cashTableView reloadData];
        [self loadUserDataA];
    }
}

//tabBar 隐藏
- (void)pushForm:(UINavigationController *)pushForm pushToAndHidden:(UIViewController *)pushToVC
{
    [pushToVC setHidesBottomBarWhenPushed:YES];
    [pushForm pushViewController:pushToVC animated:YES];
    
}


- (void)dealloc {
    
    
}
#pragma mark - 键盘的Frame改变的时候调用
- (void)keyboardFrameChangeA:(NSNotification *)note {
    
    CGFloat durtion = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    
    //获取键盘的frame
    CGRect frame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    if (frame.origin.y == HEIGHT) {//没有弹出键盘
        [UIView animateWithDuration:durtion animations:^{
            self.cashTableView.height = HEIGHT;
        }];
    } else {//弹出键盘
        [UIView animateWithDuration:durtion animations:^{
            //工具条往上移动271
            self.cashTableView.height = HEIGHT - frame.size.height-65;
        }];
    }
    //    WSYLog(@"%@", note.userInfo);
}

@end
